<script setup>
import {reactive} from "vue";
import {Search} from "@element-plus/icons-vue";
import request from "@/utils/request";
import {ElMessage, ElMessageBox} from "element-plus";

/**
 * 本地存储的账户信息
 *
 * @type {any}
 */
const account = JSON.parse(localStorage.getItem('account-info') || '{}')

/**
 * 学院列表
 * @type {*[]}
 */
let collageOptions = []

/**
 * 房间列表
 * @type {*[]}
 */
let roomOptions = []

/**
 * 教师数据
 *
 * @type {UnwrapNestedRefs<{total: number, teacherName: string, form: {teacherEmail: string, teacherAge: number, teacherName: string, teacherCollageNo: string, teacherCountNumber: string, teacherCreateMan: string, teacherRoomName: string, teacherNo: string, teacherCreateTime: string, teacherUpdateMan: string, teacherCollageName: string, teacherRoomNo: string, teacherPhone: string, teacherSex: string, teacherUpdateTime: string, teacherPositionalTitle: string}, pageSize: number, tableData: *[], pageNum: number}>}
 */
const teacherData = reactive({
    teacherName: '',
    tableData: [],
    total: 0,
    pageSize: 4,
    pageNum: 1,
    formVisible: false, // 是否显示弹窗
    form: {
        teacherNo: '',
        teacherName: '',
        teacherAge: 0,
        teacherSex: '',
        teacherPositionalTitle: '',
        teacherCollageNo: '',
        teacherCollageName: '',
        teacherRoomNo: '',
        teacherRoomName: '',
        teacherPhone: '',
        teacherEmail: '',
        teacherCreateMan: '',
        teacherCreateTime: '',
        teacherUpdateMan: '',
        teacherUpdateTime: '',
        teacherCountNumber: '',
    }
})

/**
 * 加载教师
 */
const loadTeacher = () => {
    request.get(`/api/teacher/find-teacher-page`, {
        params: {
            pageNum: teacherData.pageNum,
            pageSize: teacherData.pageSize,
            teacherName: teacherData.teacherName
        }
    }).then(res => {
        teacherData.tableData = res.data?.list || []
        teacherData.total = res.data?.list.map(item => item.teacherCountNumber)[0] || 0;
    })
}
// 调用教师加载方法，获取后台数据
loadTeacher()

/**
 * 翻页
 */
const handelCurrentChange = () => {
    // 翻页时，重新加载数据即可
    loadTeacher()
}

/**
 * 重置
 */
const reset = () => {
    teacherData.teacherName = ''
    // 重置时，重新加载数据即可
    loadTeacher()
}

/**
 * 删除教师
 *
 * @param teacherNo 工号
 */
const deleteTeacher = (teacherNo) => {
    ElMessageBox.confirm("删除数据后无法恢复，您确认删除此条数据？", "删除确认", {type: 'warning'}).then(() => {
        if (account.accountRoleName === 'visitor-admin') {
            ElMessage.info("游客-管理员仅可以查看信息")
        } else {
            request.delete(`/api/teacher/delete-teacher/${teacherNo}`).then(res => {
                if (res.code === "200") {
                    // 重新获取数据
                    loadTeacher()
                    ElMessage.success("删除成功")
                } else {
                    ElMessage.error(res.msg)
                }
            })
        }
    }).catch(() => {
        ElMessage({
            type: "info",
            message: "取消删除",
        })
    })
}

/**
 * 加载学院
 */
const loadCollage = () => {
    request.get(`/api/collage/find-collage`, {}).then(res => {
        collageOptions = res.data
    })
}
// 调用学院方法，获取后台数据
loadCollage()


/**
 * 加载房间
 */
const loadRoom = () => {
    request.get(`/api/room/find-room`, {}).then(res => {
        roomOptions = res.data
    })
}
// 调用房间方法，获取后台数据
loadRoom()

/**
 * 修改教师
 *
 * @param row 按行修改
 */
const editTeacher = (row) => {
    teacherData.form.teacherNo = row.teacherNo
    teacherData.form.teacherName = row.teacherName
    teacherData.form.teacherRoomNo = row.teacherRoomNo
    teacherData.form.teacherRoomName = row.teacherRoomName
    teacherData.form.teacherCollageNo = row.teacherCollageNo
    teacherData.form.teacherCollageName = row.teacherCollageName
    teacherData.form.teacherPositionalTitle = row.teacherPositionalTitle
    teacherData.formVisible = true
}

/**
 * 提交修改的教师
 */
const submitEditTeacher = () => {
    if (account.accountRoleName === 'visitor-admin') {
        ElMessage.info("游客-管理员仅可以查看信息")
    } else {
        teacherData.form.teacherUpdateMan = account.accountNo
        request.put(`/api/teacher/update-teacher`, teacherData.form).then(res => {
            if (res.code === "200") {
                // 重新获取数据
                loadTeacher()
                // 关闭弹窗
                teacherData.formVisible = false
                ElMessage.success("编辑成功")
            } else {
                ElMessage.error(res.msg)
            }
        })
    }
}

/**
 * 上传成功
 */
const uploadSuccess = () => {
    if (account.accountRoleName === 'visitor-admin') {
        ElMessage.info("游客-管理员仅可以查看信息")
    } else {
        console.log("上传成功");
        ElMessage.success("上传成功")
        // 重新获取数据
        loadTeacher()
    }
}
</script>

<template>
    <div v-if="account.accountRoleName === 'system-admin' || account.accountRoleName === 'visitor-admin'">
        <div class="card" style="margin-bottom: 10px;">
            <el-input style="width: 300px; margin-right: 10px;" v-model="teacherData.teacherName"
                      placeholder="请输入姓名查询"
                      :prefix-icon="Search"/>
            <el-button @click="loadTeacher" type="primary" style="margin-left: 10px;">查询</el-button>
            <el-button @click="reset" type="info">重置</el-button>
        </div>
        <div class="card" style="margin-bottom: 10px;">
            <div>
                <el-upload class="upload-demo"
                           action="http://localhost:9090/api/system/upload-teacher-excel"
                           accept=".xlsx"
                           :show-file-list="false"
                           :on-success="uploadSuccess">
                    <el-button type="primary" v-if="account.accountRoleName === 'system-admin'">导入Excel</el-button>
                </el-upload>
                <el-button type="primary" @click="uploadSuccess" v-if="account.accountRoleName === 'visitor-admin'">
                    导入Excel
                </el-button>
                <div class="el-upload__tip">
                    仅允许上传一个文件，仅限xlsx格式
                </div>
                <el-table :data="teacherData.tableData" style="width: 100%">
                    <el-table-column prop="teacherNo" label="工号" width="70px"/>
                    <el-table-column prop="teacherName" label="姓名" width="80px"/>
                    <el-table-column prop="teacherAge" label="年龄" width="60px"/>
                    <el-table-column prop="teacherSex" label="性别" width="60px"/>
                    <el-table-column prop="teacherPositionalTitle" label="职称"/>
                    <el-table-column prop="teacherCollageName" label="学院"/>
                    <el-table-column prop="teacherRoomName" label="办公室"/>
                    <el-table-column prop="teacherPhone" label="手机号" width="105px"/>
                    <el-table-column prop="teacherEmail" label="电子邮箱"/>
                    <el-table-column label="操作">
                        <template #default="scope">
                            <el-button @click="editTeacher(scope.row)" type="primary" size="small" plain>编辑
                            </el-button>
                            <el-button @click="deleteTeacher(scope.row.teacherNo)" type="danger" size="small"
                                       plain>删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </div>
        <div class="card">
            <el-pagination v-model:current-page="teacherData.pageNum"
                           v-model:page-size="teacherData.pageSize"
                           @current-change="handelCurrentChange"
                           background layout="prev, pager, next" :total="teacherData.total"/>
        </div>

        <el-dialog v-model="teacherData.formVisible" :close-on-click-modal="false" title="教师信息">
            <el-form :model="teacherData.form" label-width="100px" label-position="right"
                     style="padding-right: 30px;">
                <el-form-item label="工号">
                    <el-input placeholder="未填写" v-model="teacherData.form.teacherNo"
                              autocomplete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="姓名">
                    <el-input placeholder="未填写" v-model="teacherData.form.teacherName"
                              autocomplete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="职称">
                    <el-input placeholder="未填写" v-model="teacherData.form.teacherPositionalTitle"
                              autocomplete="off"></el-input>
                </el-form-item>
                <div class="block" style="margin-bottom: 20px;margin-left: 45px">
                    <el-select
                            v-model="teacherData.form.teacherCollageNo"
                            class="m-2"
                            placeholder="请选择学院"
                            style="width: 525px; margin-bottom: 20px;"
                    >
                        <el-option
                                v-for="item in collageOptions"
                                :key="item.collageNo"
                                :label="item.collageName"
                                :value="item.collageNo"
                        />
                    </el-select>
                    <el-select
                            v-model="teacherData.form.teacherRoomNo"
                            class="m-2"
                            placeholder="请选择办公室"
                            style="width: 525px;"
                    >
                        <el-option
                                v-for="item in roomOptions"
                                :key="item.roomNo"
                                :label="item.roomName"
                                :value="item.roomNo"
                        />
                    </el-select>
                </div>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="teacherData.formVisible = false">取消</el-button>
                    <el-button type="primary" @click="submitEditTeacher">提交</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<style scoped>

</style>